package com.example.jiankang.pages;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.jiankang.R;

public class zhuceActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_zhuce);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //获取mSharedPreferences
        mSharedPreferences = getSharedPreferences("user",MODE_PRIVATE);

        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        ImageView backimage = findViewById(R.id.image_back);

        backimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(zhuceActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.button_zhuce).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username =  et_username.getText().toString();
                String password =  et_password.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
                    Toast.makeText(zhuceActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }else{
                    SharedPreferences.Editor edit = mSharedPreferences.edit();
                    edit.putString("username",username);
                    edit.putString("password",password);
                    edit.commit();
                    Toast.makeText(zhuceActivity.this, "注册成功，请登录", Toast.LENGTH_SHORT).show();
                    finish();
                }

            }
        });
    }
}